const { uploadDir, createMD5, writeData, readData, pushLog } = require('../common/operate');
var { APIOK, APIError } = require('../common/status');
var fs = require('fs');
var path = require('path');

var fn_create = async (ctx, next) => {
    let id = ctx.params.id
    if (id && id.startsWith('id_') && id.length == 18) {
        let userPath = path.join(uploadDir, `/${id}/`)
        if (!fs.existsSync(userPath)) {
            fs.mkdirSync(userPath)
            let time = new Date().getTime().toString()
            let token = createMD5(`${id}|${time}`)
            if (writeData(id, token)) {
                pushLog(id, 0, 0, '创建目录', '成功')
                APIOK(ctx, {
                    token
                })
            }
        } else {
            pushLog(id, 0, 0, '进入目录', '成功')
            APIOK(ctx, {
                token: readData(id)
            })
        }
    } else {
        pushLog(id, 0, 0, '创建目录', '失败')
        APIError(ctx, 400, '参数错误')
    }
}

module.exports = fn_create